package com.rbac.api.mybatis.mapper;


import com.rbac.api.controller.request.PostMenusListRequest;
import com.rbac.api.mybatis.entity.AdminMenusEntity;
import com.rbac.api.mybatis.entityCus.CusHaveMenusEntity;
import com.rbac.api.mybatis.sqlProvider.AdminMenusSqlProvider;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.Date;
import java.util.List;

public interface AdminMenusMapper {

    ///根据管理员编号获取拥有的菜单list
    @SelectProvider(type=AdminMenusSqlProvider.class,method = "getHaveMenusListUserNoSelective")
    @Results({
            @Result(column="user_no", property="userNo", jdbcType= JdbcType.VARCHAR),
            @Result(column="role_no", property="roleNo", jdbcType=JdbcType.VARCHAR),
            @Result(column="menu_no", property="menuNo", jdbcType=JdbcType.VARCHAR),
            @Result(column="parent_no", property="parentNo", jdbcType=JdbcType.VARCHAR),
            @Result(column="menu_address", property="menuAddress", jdbcType=JdbcType.VARCHAR),
            @Result(column="menu_name", property="menuName", jdbcType=JdbcType.VARCHAR),
            @Result(column="menu_sort", property="menuSort", jdbcType=JdbcType.BIGINT)
    })
    List<CusHaveMenusEntity> getHaveMenusListUserNoSelective(@Param("userNo") String userNo);




    ///获取菜单列表数据条数
    @SelectProvider(type=AdminMenusSqlProvider.class,method = "getMenusListCountSelective")
    Integer getMenusListCountSelective(@Param("paramRequest") PostMenusListRequest menusListRequest);
    ///获取菜单列表数据list
    @SelectProvider(type=AdminMenusSqlProvider.class,method = "getMenusListDataSelective")
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.BIGINT),
            @Result(column="menu_no", property="menuNo", jdbcType=JdbcType.VARCHAR),
            @Result(column="parent_no", property="parentNo", jdbcType=JdbcType.VARCHAR),
            @Result(column="menu_name", property="menuName", jdbcType=JdbcType.VARCHAR),
            @Result(column="menu_address", property="menuAddress", jdbcType=JdbcType.VARCHAR),
            @Result(column="menu_sort", property="menuSort", jdbcType=JdbcType.BIGINT),
            @Result(column="menu_status", property="menuStatus", jdbcType=JdbcType.INTEGER)
    })
    List<AdminMenusEntity> getMenusListDataSelective(@Param("paramRequest") PostMenusListRequest menusListRequest, @Param("StartRow") Integer StartRow, @Param("pageSize") Integer pageSize);



    ///新增菜单，并返回创建的数据id
    @Insert({
            "insert into admin_menus (`menu_no`, `parent_no`, ",
            "`menu_name`, `menu_address`,`menu_sort`,",
            "`create_uid`,`create_time`) ",
            "values (#{menuNo,jdbcType=VARCHAR}, #{parentNo,jdbcType=VARCHAR}, ",
            "#{menuName,jdbcType=VARCHAR}, #{menuAddress,jdbcType=VARCHAR},#{menuSort,jdbcType=BIGINT},",
            "#{createUid,jdbcType=VARCHAR},#{createTime,jdbcType=TIMESTAMP}) "
    })
    @Options(useGeneratedKeys = true, keyProperty = "id")
    Integer menuAddMapper(AdminMenusEntity record);



    ///新增菜单（获取顶级菜单列表）
    @Select({
            "select\n",
            "menu_no,parent_no,menu_name\n",
            "FROM admin_menus\n",
            "WHERE menu_status = '2' and parent_no='0'"
    })
    @Results({
            @Result(column="menu_no", property="menuNo", jdbcType=JdbcType.VARCHAR),
            @Result(column="parent_no", property="parentNo", jdbcType=JdbcType.VARCHAR),
            @Result(column="menu_name", property="menuName", jdbcType=JdbcType.VARCHAR)
    })
    List<AdminMenusEntity> getTopMenuListMapper();


    ///菜单状态修改
    ///修改后的状态、当前操作人编号、操作时间、数据id、数据当前状态
    @Update({
            "update admin_menus",
            "set menu_status = #{menuStatus,jdbcType=INTEGER},",
            "last_update_uid = #{lastUpdateUid,jdbcType=VARCHAR},",
            "last_update_time = #{lastUpdateTime,jdbcType=TIMESTAMP}",
            "where id = #{id,jdbcType=INTEGER}",
            "and menu_status = #{nowStatus,jdbcType=INTEGER}"
    })
    Integer saveStatusByIdAndStatusMapper(@Param("menuStatus") Integer menuStatus, @Param("lastUpdateUid") String lastUpdateUid, @Param("lastUpdateTime") Date lastUpdateTime, @Param("id") Integer id, @Param("nowStatus") Integer nowStatus);




    ///角色操作获取可操作的且状态为有效的菜单list
    @Select({
            "select\n",
            "menu_no,parent_no,menu_name\n",
            "FROM admin_menus\n",
            "WHERE menu_status = '2'\n",
            "and parent_no !='-1'"
    })
    @Results({
            @Result(column="menu_no", property="menuNo", jdbcType=JdbcType.VARCHAR),
            @Result(column="parent_no", property="parentNo", jdbcType=JdbcType.VARCHAR),
            @Result(column="menu_name", property="menuName", jdbcType=JdbcType.VARCHAR)
    })
    List<AdminMenusEntity> actionMenusListMapper();




    ///根据菜单地址获取菜单编号
    @SelectProvider(type=AdminMenusSqlProvider.class, method="selectMenuNoByMenuAddressSelective")
    String selectMenuNoByMenuAddressSelective(@Param("path") String path);



    ///根据管理员编号和菜单编号count是否拥有此地址权限
    @SelectProvider(type=AdminMenusSqlProvider.class, method="checkThisAddressPowerSelective")
    Integer checkThisAddressPowerSelective(@Param("userNo") String userNo,@Param("menuNo") String menuNo);



}